package com.itheima.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TTLConfiguration
 * 配置延迟主队列
 *
 * @author tianyuchan
 * @since 2024/7/2
 */

@Configuration
public class TTLConfiguration {
    // 声明延迟队列
    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl.direct");
    }
    // 声明延迟队列并绑定死信交换机
    @Bean
    public Queue ttlQueue() {
        return QueueBuilder
                .durable("ttl.queue")
                .ttl(10000)
                // 绑定死信交换机时不指定死信路由key，则默认沿用主队列的路由key
                .deadLetterExchange("dlx.direct")
                // 指定死信交换机转发死信队列时的路由key
                .deadLetterRoutingKey("ha")
                .build();
    }
    // 将延迟队列绑定到延迟交换机
    @Bean
    public Binding ttlDirectExchangeBinding(DirectExchange ttlDirectExchange, Queue ttlQueue) {
        return BindingBuilder.bind(ttlQueue).to(ttlDirectExchange).with("hi");
    }

    // 死信交换机
    @Bean
    public DirectExchange dlxDirectExchange() {
        return new DirectExchange("dlx.direct");
    }
    // 死信队列
    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue");
    }
    // 将死信队列绑定到死信交换机
    @Bean
    public Binding dlxDirectExchangeBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxDirectExchange()).with("hi");
    }

    // 死信队列2
    @Bean
    public Queue dlxQueue2() {
        return new Queue("dlx.queue2");
    }
    // 将死信队列2绑定到死信交换机
    @Bean
    public Binding dlxDirectExchangeBinding2() {
        return BindingBuilder.bind(dlxQueue2()).to(dlxDirectExchange()).with("ha");
    }


}
